home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / elk-2_0.lha / elk-2.0 / src / dump.ecoff.c < prev    next >
C/C++ Source or Header  |  1992-11-08  |  6KB  |  186 lines

  1. #include <sys/file.h>
  2. #include <sys/types.h>
  3. #include <filehdr.h>
  4. #include <aouthdr.h>
  5. #include <scnhdr.h>
  6. #include <sym.h>
  7.  
  8. extern char *sbrk();
  9.  
  10. #define BADAOUT(err_msg) {\
  11.     Dump_Finalize;\
  12.     Primitive_Error (err_msg);\
  13. }
  14.  
  15. #define CHECK_SCNHDR(ptr, name, flags) {\
  16.     if (strcmp (hdr.section[i].s_name, name) == 0) {\
  17.     if (hdr.section[i].s_flags != flags)\
  18.         Primitive_Error ("bad flags in section header");\
  19.         ptr = hdr.section + i;\
  20.     i++;\
  21.     } else {\
  22.     ptr = 0;\
  23.     }\
  24. }
  25.  
  26. #define WRITE(fd, buf, size, errmsg)\
  27.     if (write (fd, buf, size) != size) {\
  28.     Dump_Finalize;\
  29.     Primitive_Error (errmsg);\
  30.     }
  31.  
  32. #define SEEK(fd, pos, errmsg)\
  33.     if (lseek (fd, pos, L_SET) != pos) {\
  34.     Dump_Finalize;\
  35.     Primitive_Error (errmsg);\
  36.     }
  37.  
  38. #define BUFSIZE 8192
  39.  
  40. static struct scnhdr *text_section;
  41. static struct scnhdr *init_section;
  42. static struct scnhdr *rdata_section;
  43. static struct scnhdr *data_section;
  44. static struct scnhdr *lit8_section;
  45. static struct scnhdr *lit4_section;
  46. static struct scnhdr *sdata_section;
  47. static struct scnhdr *sbss_section;
  48. static struct scnhdr *bss_section;
  49. static struct scnhdr *rsrc_section;
  50.  
  51. struct headers {
  52.     struct filehdr fhdr;
  53.     struct aouthdr aout;
  54.     struct scnhdr section[10];
  55. };
  56.  
  57. Object P_Dump (ofile) Object ofile; {
  58.     char buf[BUFSIZE];
  59.     int i;            /* a simple counter */
  60.     int newsyms;
  61.     int symrel;
  62.     int nread;            /* number of bytes read */
  63.     int scnptr;            /* the section being worked on */
  64.     int vaddr;
  65.     int pagesize;
  66.     unsigned data_end;
  67.     static struct headers hdr;
  68.  
  69.     Dump_Prolog;
  70.  
  71.     hdr = *((struct headers *)TEXT_START);
  72.   
  73.     if (hdr.fhdr.f_magic != MIPSELMAGIC && hdr.fhdr.f_magic != MIPSEBMAGIC)
  74.     BADAOUT("input file has bad magic number");
  75.  
  76.     if (hdr.fhdr.f_opthdr != sizeof(hdr.aout))
  77.     BADAOUT("input file's a.out header has the wrong size");
  78.   
  79.     if (hdr.aout.magic != ZMAGIC)
  80.     BADAOUT("input file is not a ZMAGIC file");
  81.  
  82.     i = 0;
  83.     CHECK_SCNHDR(text_section,  _TEXT,     STYP_TEXT);
  84.     CHECK_SCNHDR(init_section,  _INIT,     STYP_INIT);
  85.     CHECK_SCNHDR(rdata_section, _RDATA,    STYP_RDATA);
  86.     CHECK_SCNHDR(data_section,  _DATA,     STYP_DATA);
  87. #ifdef _LIT8
  88.     CHECK_SCNHDR(lit8_section,  _LIT8,     STYP_LIT8);
  89.     CHECK_SCNHDR(lit4_section,  _LIT4,     STYP_LIT4);
  90. #endif
  91.     CHECK_SCNHDR(sdata_section, _SDATA,    STYP_SDATA);
  92.     CHECK_SCNHDR(sbss_section,  _SBSS,     STYP_SBSS);
  93.     CHECK_SCNHDR(bss_section,   _BSS,      STYP_BSS);
  94. #ifdef _RESOURCE
  95.     CHECK_SCNHDR(rsrc_section,  _RESOURCE, _STYP_RESOURCE);
  96. #endif
  97.     if (i != hdr.fhdr.f_nscns)
  98.     Primitive_Error ("~s sections expected, ~s found",
  99.         Make_Fixnum (hdr.fhdr.f_nscns), Make_Fixnum (i));
  100.  
  101.     Was_Dumped = 1;
  102.  
  103.     pagesize = getpagesize();
  104.     data_end = ((unsigned)sbrk (0) + pagesize - 1) & (-pagesize);
  105.     hdr.aout.dsize = data_end - DATA_START;
  106.     hdr.aout.bsize =0;
  107.   
  108.     {
  109.     extern __start();
  110.     hdr.aout.entry = (unsigned)__start;
  111.     }
  112.  
  113.     hdr.aout.bss_start = hdr.aout.data_start + hdr.aout.dsize;
  114.  
  115.     data_section->s_vaddr = DATA_START + rdata_section->s_size;
  116.     data_section->s_paddr = DATA_START + rdata_section->s_size;
  117.  
  118.     data_section->s_size = data_end - DATA_START;
  119.     data_section->s_scnptr = rdata_section->s_scnptr + rdata_section->s_size;
  120.  
  121.     vaddr = data_section->s_vaddr + data_section->s_size;
  122.     scnptr = data_section->s_scnptr + data_section->s_size;
  123.     if (lit8_section) {
  124.     lit8_section->s_vaddr = data_section->s_vaddr + data_section->s_size;
  125.     lit8_section->s_paddr = data_section->s_paddr + data_section->s_size;
  126.     lit8_section->s_scnptr = data_section->s_scnptr + data_section->s_size;
  127.     }
  128.     if (sdata_section && lit8_section) {
  129.     sdata_section->s_vaddr = lit8_section->s_vaddr + lit8_section->s_size;
  130.     sdata_section->s_paddr = lit8_section->s_paddr + lit8_section->s_size;
  131.     sdata_section->s_scnptr = lit8_section->s_scnptr + lit8_section->s_size;
  132.     }
  133.     if (sbss_section) {
  134.     sbss_section->s_vaddr = sdata_section->s_vaddr + sdata_section->s_size;
  135.     sbss_section->s_paddr = sdata_section->s_paddr + sdata_section->s_size;
  136.     sbss_section->s_scnptr = sdata_section->s_scnptr+sdata_section->s_size;
  137.     }
  138.     if (bss_section) {
  139.     bss_section->s_vaddr = sbss_section->s_vaddr + sbss_section->s_size;
  140.     bss_section->s_paddr = sbss_section->s_paddr + sbss_section->s_size;
  141.     bss_section->s_scnptr = sbss_section->s_scnptr + sbss_section->s_size;
  142.     }
  143.  
  144.     WRITE(ofd, TEXT_START, hdr.aout.tsize,
  145.     "problem writing text section to output file: ~E");
  146.  
  147.     WRITE(ofd, DATA_START, hdr.aout.dsize,
  148.     "problem writing data section to output file: ~E");
  149.   
  150.     /* Set up and copy the symbol table to the new executable
  151.     */
  152.     SEEK(afd, hdr.fhdr.f_symptr, "can't seek to start of symbol table: ~E");
  153.  
  154.     if ((nread = read (afd, buf, BUFSIZE)) < sizeof (HDRR))
  155.     Primitive_Error ("problem reading symbol table");
  156.  
  157. #define symhdr ((pHDRR)buf)
  158.     newsyms = hdr.aout.tsize + hdr.aout.dsize;
  159.     symrel = newsyms - hdr.fhdr.f_symptr;
  160.     hdr.fhdr.f_symptr = newsyms;
  161.     symhdr->cbLineOffset += symrel;
  162.     symhdr->cbDnOffset += symrel;
  163.     symhdr->cbPdOffset += symrel;
  164.     symhdr->cbSymOffset += symrel;
  165.     symhdr->cbOptOffset += symrel;
  166.     symhdr->cbAuxOffset += symrel;
  167.     symhdr->cbSsOffset += symrel;
  168.     symhdr->cbSsExtOffset += symrel;
  169.     symhdr->cbFdOffset += symrel;
  170.     symhdr->cbRfdOffset += symrel;
  171.     symhdr->cbExtOffset += symrel;
  172. #undef symhdr
  173.  
  174.     do {
  175.     if (write (ofd, buf, nread) != nread)
  176.         Primitive_Error ("problem writing symbols to output file");
  177.     if ((nread = read (afd, buf, BUFSIZE)) < 0)
  178.         Primitive_Error ("problem reading symbols");
  179.     } while (nread != 0);
  180.  
  181.     SEEK(ofd, 0L, "problem seeking to start of header: ~E");
  182.     WRITE(ofd, &hdr, sizeof(hdr), "problem writing header of output file: ~E");
  183.  
  184.     Dump_Epilog;
  185. }
  186.